package com.jierui.meter.frontend.service.impl;

import com.jierui.meter.frontend.domain.BeanUtil;
import com.jierui.meter.frontend.domain.DO.*;
import com.jierui.meter.frontend.domain.ExceptionCodeEnum;
import com.jierui.meter.frontend.domain.Param.*;
import com.jierui.meter.frontend.domain.VO.*;
import com.jierui.meter.frontend.mapper.*;
import com.jierui.meter.frontend.service.TBuComputingIntervalService;
import com.jierui.meter.frontend.service.TErrorComputingService;
import com.jierui.meter.frontend.service.TgStatusService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @description:
 * @author: wangjingjing
 * @creat: 2021-06-17 20:00
 */
@Service
public class TErrorComputingServiceImpl implements TErrorComputingService {


    @Autowired
    TErrorComputingMapper tErrorComputingMapper;

    @Autowired
    TBuComputingIntervalService tBuComputingIntervalService;

    @Autowired
    TgInfoMapper tgInfoMapper;


    @Autowired
    SaOrgMapper saOrgMapper;

    @Autowired
    TgDailyFreezeMapper tgDailyFreezeMapper;

    @Autowired
    TBuComputingIntervalMapper tBuComputingIntervalMapper;

    @Autowired
    TgStatusMapper tgStatusMapper;

    @Autowired
    MeterStatusMapper meterStatusMapper;

    @Autowired
    TgStatusService tgStatusService;

    public final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    public final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd");

    @Override
    public Map<String, Object> selectErrorAndMeter(String calerror, String checkstatus, String orgInput,
                                                   String idInput, String year, String scope, String orgNo7,
                                                   Integer page, Integer size) {

        Map<String, Object> result = new HashMap<>();
        //查询数据库
        List<TErrorComputingAndMeterDO> dos = tErrorComputingMapper.selectErrorAndMeter(calerror, checkstatus, orgInput, idInput, year, scope, orgNo7, (page - 1) * size, size);
        int count = tErrorComputingMapper.selectErrorAndMeterCount(calerror, checkstatus, orgInput, idInput, year, scope, orgNo7);
        List<TErrorComputingAndMeterVO> data = new ArrayList<>();
        dos.stream().forEach(errDO -> {
            data.add(BeanUtil.ErrorDO2VO(errDO));
        });
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));
        result.put("list", data);
        return result;
    }

    @Override
    public Map<String, Object> selectAllMeterLike(String orgInput, String idInput, String year, String scope, String orgNo7, Integer page, Integer size, Integer status) {
        Map<String, Object> result = new HashMap<>();

        //查询meter_status 数据
        MeterStatusParam meterStatusParam = new MeterStatusParam();
        meterStatusParam.setOrgNo7(orgNo7);
        meterStatusParam.setOrgNameLike(orgInput);
        meterStatusParam.setMeterIdOrAssnetLike(idInput);
        meterStatusParam.setStatus(status);
        meterStatusParam.setPage((page - 1) * size);
        meterStatusParam.setSize(size);

        List<MeterStatusDO> meterStatusDOS = meterStatusMapper.selectByParamLeftSaOrg(meterStatusParam);
        int count = meterStatusMapper.selectByParamLeftSaOrgCount(meterStatusParam);
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));
        List<AllMeterByLikeVO> data = new ArrayList<>();
        if (count == 0) {
            result.put("list", data);
        }
        for (MeterStatusDO meterStatus : meterStatusDOS) {
            AllMeterByLikeVO vo = BeanUtil.meterStatus2AllMeterByLikeVO(meterStatus);
            data.add(vo);
        }
        result.put("list", data);
        return result;
    }


    @Override
    public Map<String, Object> selectByAlarmCode(String year, Integer page, Integer size, String orgNo, int distinctType) {
        Map<String, Object> result = new HashMap<>();
        List<TErrorComputingResultsDO> dos = new ArrayList<>();
        ErrorComputingParam param = new ErrorComputingParam();
        param.setYear(year);
        param.setOrgNo(orgNo);
        param.setSize(size);
        param.setPage((page - 1) * size);
        int count = 0;
        if (distinctType == 1) {
            dos = tErrorComputingMapper.selectByAlarmCodeDistinctMeterId(param);
            count = tErrorComputingMapper.selectByAlarmCodeDistinctMeterIdCount(year, orgNo);
        } else if (distinctType == 2) {
            dos = tErrorComputingMapper.selectByAlarmCodeDistinctTgId(param);
            count = tErrorComputingMapper.selectByAlarmCodeDistinctTgIdCount(year, orgNo);
        }
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));
        List<StatisticsByExceptionVO> data = new ArrayList<>();


        if (dos.isEmpty()) {
            result.put("list", data);
            return result;
        }

        //查询所有异常数量
        int errorNum = 0;
        if (!dos.isEmpty()) {
            errorNum = dos.stream().map(errorDO -> errorDO.getAlarmNum()).reduce(Integer::sum).get();
        }

        for (TErrorComputingResultsDO errorDO : dos) {
            StatisticsByExceptionVO vo = new StatisticsByExceptionVO();
            vo.setAlarm_code(errorDO.getAlarmCode());
            vo.setAlarm_name(ExceptionCodeEnum.getName(errorDO.getAlarmCode()));
            vo.setAlarm_num(errorDO.getAlarmNum());
            if (errorNum > 0) {
                //台区当前codeErr / AllErr
                vo.setAlarm_rate(Math.round(errorDO.getAlarmNum() / (float) errorNum * 10000) / 100F);
            } else {
                vo.setAlarm_rate(0);
            }
            data.add(vo);
        }

        result.put("list", data);
        return result;

    }


    @Override
    public List<StatisticsByExceptionVO> selectByAlarmCodeNoPage(String year, String orgNo7, int distinctType) {
        List<TErrorComputingResultsDO> dos = new ArrayList<>();
        ErrorComputingParam errorParam = new ErrorComputingParam();
        errorParam.setOrgNo7(orgNo7);
        errorParam.setYear(year);
        if (distinctType == 1) {
            dos = tErrorComputingMapper.selectByAlarmCodeDistinctMeterId(errorParam);
        } else if (distinctType == 2) {
            dos = tErrorComputingMapper.selectByAlarmCodeDistinctTgId(errorParam);
        }
        //当前页
        List<StatisticsByExceptionVO> data = new ArrayList<>();


        if (dos.isEmpty()) {
            return data;
        }

        //查询所有异常数量
        int errorNum = 0;
        //求和
        errorNum = dos.stream().map(errorDO -> errorDO.getAlarmNum()).reduce(Integer::sum).get();

        for (TErrorComputingResultsDO errorDO : dos) {
            StatisticsByExceptionVO vo = new StatisticsByExceptionVO();
            vo.setAlarm_code(errorDO.getAlarmCode());
            vo.setAlarm_name(ExceptionCodeEnum.getName(errorDO.getAlarmCode()));
            vo.setAlarm_num(errorDO.getAlarmNum());
            if (errorNum > 0) {
                //台区当前codeErr / AllErr
                vo.setAlarm_rate(Math.round((errorDO.getAlarmNum() / (float) errorNum) * 10000) / 10000f);
            } else {
                vo.setAlarm_rate(0);
            }
            data.add(vo);
        }

        return data;

    }

    /**
     * 3.1电能表按供电单位统计接口
     *
     * @param year
     * @param scope
     * @param orgNo7
     * @return
     */
    @Override
    public Map<String, Object> statisticsByPowerSupplyUnit(String year, String scope, String orgNo7, Integer page, Integer size) {

        Map<String, Object> result = new HashMap<>();

        List<TBuComputingIntervalVO> data = new ArrayList<>();


        SaOrgParam saOrgParam = new SaOrgParam();
        saOrgParam.setOrgNo7(orgNo7);
        saOrgParam.setPage((page - 1) * size);
        saOrgParam.setSize(size);
        //查询Org
        if (StringUtils.isBlank(orgNo7)) {
            //查询orn7 的供电所
            saOrgParam.setOrgType(4);
        }
        List<SaOrgDO> saOrgDOS = saOrgMapper.selectByParam(saOrgParam);
        int count = saOrgMapper.selectByParamCount(saOrgParam);
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));
        //如果没有数据 则返回空
        if (saOrgDOS.isEmpty()) {
            result.put("list", data);
            return result;
        }

        List<String> orgNo7s = saOrgDOS.stream().map(SaOrgDO::getOrgNo).collect(Collectors.toList());
        //tg_status left tg_info GROUP BY ORGnO 查询正常表异常表  观察表
        TgStatusParam statusParam = new TgStatusParam();
        statusParam.setYear(year);
        statusParam.setOrgNo7s(orgNo7s);

        //查询正常台区数量 按供电公司分组 status 1 为正常
        List<TgStatusDO> normalStatusDOS = tgStatusMapper.selectByParamGroupByOrgNo7(statusParam);

        Map<String, TgStatusDO> meterNumMap = new HashMap<>();

        if (!normalStatusDOS.isEmpty()) {
            meterNumMap = normalStatusDOS.stream().collect(Collectors.toMap(TgStatusDO::getORGNO, Function.identity()));
        }

        for (SaOrgDO saOrgDO : saOrgDOS) {
            TBuComputingIntervalVO vo = new TBuComputingIntervalVO();
            vo.setOrg_id(saOrgDO.getOrgNo());
            if (StringUtils.isNotBlank(saOrgDO.getOrgName())) {
                vo.setOrg_7_name(saOrgDO.getOrgName().trim());
            }
            if (meterNumMap.containsKey(saOrgDO.getOrgNo())) {
                TgStatusDO tgStatusDO = meterNumMap.get(saOrgDO.getOrgNo());
                //该供电单位下有电能表
                vo.setMeter_num(tgStatusDO.getNMETERS());
                //正常表
                vo.setNormal_meter_num(tgStatusDO.getNMETERS() - tgStatusDO.getNEMETERS());
                vo.setNormal_meter_rate(Math.round(vo.getNormal_meter_num() * 1F / vo.getMeter_num() * 10000F) / 100F);
                //异常表
                vo.setInvalid_meter_num(tgStatusDO.getNIMETERS());
                vo.setInvalid_meter_rate(Math.round(vo.getInvalid_meter_num() * 1F / vo.getMeter_num() * 10000F) / 100F);
                //观察表
                vo.setTest_meter_num(tgStatusDO.getNMETERS() - tgStatusDO.getNIMETERS());
                vo.setTest_meter_rate(Math.round(vo.getTest_meter_num() * 1F / vo.getMeter_num() * 10000F) / 100F);
            }
            data.add(vo);
        }


        result.put("list", data);
        return result;
    }


    /**
     * 3.1电能表按供电单位统计接口(无分页)
     *
     * @param year
     * @param scope
     * @param orgNo7
     * @return
     */
    @Override
    public List<TBuComputingIntervalVO> statisticsByPowerSupplyUnitNoPage(String year, String scope, String orgNo7) {


        //查询电能表总数
        UniversalParam param = new UniversalParam();
        param.setYear(year);
        param.setScope(scope);
        param.setOrgNo7(orgNo7);
        param.setGroupByCityOrgName(1);
        List<TBuComputingIntervalDO> meterList = tBuComputingIntervalService.getTotalMeterNum(param);
        List<TBuComputingIntervalVO> data = new ArrayList<>();

        //如果没有数据 则返回空
        if (meterList.isEmpty()) {
            return data;
        }
        List<String> cityOrgNameList = meterList.stream().map(dto -> dto.getOrg7Name()).collect(Collectors.toList());

        //查询对应电能表异常数
        List<TErrorComputingResultsDO> errorList = tErrorComputingMapper.selectInvalidMeterNumByCityOrgName(year, scope, orgNo7, cityOrgNameList);
        Map<String, TErrorComputingResultsDO> errorMap = errorList.stream().collect(Collectors.toMap(TErrorComputingResultsDO::getOrg7Name, Function.identity()));
        for (TBuComputingIntervalDO meter : meterList) {
            TBuComputingIntervalVO vo = new TBuComputingIntervalVO();
            if (StringUtils.isNotBlank(meter.getOrg7Name())) {
                vo.setOrg_7_name(meter.getOrg7Name().trim());
            }
            vo.setMeter_num(meter.getMeterNum());
            //观察表暂无数据
            vo.setTest_meter_num(0);
            vo.setTest_meter_rate(0);

            if (errorMap.containsKey(meter.getOrg7Name())) {
                TErrorComputingResultsDO errorDO = errorMap.get(meter.getOrg7Name());
                vo.setInvalid_meter_num(errorDO.getInvalidMeterNum());
                if (meter.getMeterNum() > 0) {
                    vo.setInvalid_meter_rate(Math.round(errorDO.getInvalidMeterNum() / meter.getMeterNum() * 100));
                } else {
                    vo.setInvalid_meter_rate(0);
                }
                vo.setNormal_meter_num(meter.getMeterNum() - errorDO.getInvalidMeterNum());
                vo.setNormal_meter_rate(100 - vo.getNormal_meter_rate());
            } else {
                //如果异常表没有数据 则全部电表为正常表
                vo.setInvalid_meter_num(0);
                vo.setInvalid_meter_rate(0);
                vo.setNormal_meter_num(meter.getMeterNum());
                vo.setNormal_meter_rate(100);
            }
            data.add(vo);
        }
        return data;
    }


    /**
     * 查询异常表数量 以供电单位分组
     *
     * @return
     */
    public List<TErrorComputingResultsDO> selectInvalidMeterNumByCityOrgName(String year, String scope, String tg, List<String> cityOrgNameList) {

        return tErrorComputingMapper.selectInvalidMeterNumByCityOrgName(year, scope, tg, cityOrgNameList);
    }

    /**
     * 3.8异常电能表明细接口,按节点名/电能表编号/资产编号模糊查询
     *
     * @param year
     * @param scope
     * @param orgNo7
     * @param orgInput
     * @param idInput
     * @param page
     * @param size
     * @return
     */
    @Override
    public Map<String, Object> selectByNameOrIdLike(String year, String scope, String orgNo7, String orgInput, String idInput, Integer page, Integer size) {

        Map<String, Object> result = new HashMap<>();

        List<TErrorComputingAndMeterDO> dos = tErrorComputingMapper.selectByNameOrIdLike(year, scope, orgNo7, orgInput, idInput, (page - 1) * size, size);
        int count = tErrorComputingMapper.selectByNameOrIdLikeCount(year, scope, orgNo7, orgInput, idInput);
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));


        List<TErrorComputingAndMeterVO> data = new ArrayList<>();
        if (dos.isEmpty()) {
            result.put("list", data);
            return result;
        }


        dos.stream().forEach(errorDO -> {
            data.add(BeanUtil.ErrorDO2VO(errorDO));
        });
        //处理异常类型
        result.put("list", data);
        return result;
    }

    @Override
    public Map<String, Object> getMeterInfo(String meter) {

        Map<String, Object> result = new HashMap<>();
        //查询用户信息
        MeterArchiveInfoDO meterInforInfo = meterStatusMapper.getMeterInforInfo(meter);
        MeterArchiveInfoVO meterArchiveInfoVO = new MeterArchiveInfoVO();
        TgArchiveInfoVO tgArchiveInfoVO = new TgArchiveInfoVO();
        List<MpInfoVO> mpInfoVOList = new ArrayList<>();
        if (meterInforInfo != null) {
            meterArchiveInfoVO = BeanUtil.meterDO2VO(meterInforInfo);
            tgArchiveInfoVO = this.getTgArchiveInfo(meterInforInfo.getTgId());
            //电能表计量误差信
            //查询error表数据
            List<TErrorComputingResultsDO> errorComputingResultsDOS = tErrorComputingMapper.selectErrorEGroupByErrorE(meterInforInfo.getTgId());
            if (!errorComputingResultsDOS.isEmpty()) {
                errorComputingResultsDOS.stream().forEach(errorDO -> {
                    mpInfoVOList.add(BeanUtil.error2MpInfo(errorDO));
                });
            }
        }

        result.put("mp_info", mpInfoVOList);
        result.put("meter_archive_info", meterArchiveInfoVO);

        //查询台区信息
        result.put("tg_archive_info", tgArchiveInfoVO);
        //查询
        return result;
    }

    /**
     * 3.13/4.13异常电能表信息接口
     *
     * @param meterid
     * @param page
     * @param size
     * @param tgId
     * @return
     */
    @Override
    public Map<String, Object> gerErrorInfoByMeteridOrTgId(String meterid, Integer page, Integer size, String tgId, String alarmType, String alarmTime) {
        Map<String, Object> result = new HashMap<>();
        List<TErrorComputingResultsDO> dos = tErrorComputingMapper.gerErrorInfoByMeteridOrTgId(meterid, tgId, (page - 1) * size, size, alarmType, alarmTime);
        int count = tErrorComputingMapper.gerErrorInfoByMeteridOrTgIdCount(meterid, tgId, alarmType, alarmTime);


        List<ErrorInfoByMeterIDVO> data = new ArrayList<>();
        if (!dos.isEmpty()) {
            for (TErrorComputingResultsDO errorDO : dos) {
                ErrorInfoByMeterIDVO errorInfo = BeanUtil.error2ErrorInfo(errorDO);
                errorInfo.setAlarm_name(ExceptionCodeEnum.getName(errorDO.getAlarmCode()));
                data.add(errorInfo);
            }
        }
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));
        result.put("list", data);
        return result;
    }


    @Override
    public Map<String, Object> powerSupplyGroupByOrgNo7(String year, String scope, String orgNo7, Integer page, Integer size) {


        Map<String, Object> result = new HashMap<>();
        List<PowerSupplyGroupByTgVO> data = new ArrayList<>();


        SaOrgParam saOrgParam = new SaOrgParam();
        saOrgParam.setOrgNo7(orgNo7);
        saOrgParam.setPage((page - 1) * size);
        saOrgParam.setSize(size);

        //查询TgInfo
        if (StringUtils.isBlank(orgNo7)) {
            saOrgParam.setOrgType(4);
        }
        List<SaOrgDO> saOrgDOS = saOrgMapper.selectByParam(saOrgParam);
        int count = saOrgMapper.selectByParamCount(saOrgParam);
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));

        if (saOrgDOS.isEmpty()) {
            result.put("list", data);
            return result;
        }
        List<String> orgNo7s = saOrgDOS.stream().map(info -> info.getOrgNo()).collect(Collectors.toList());
        TgStatusParam statusParam = new TgStatusParam();
        statusParam.setYear(year);
        statusParam.setOrgNo7(orgNo7);
        statusParam.setStatus(1);
        statusParam.setOrgNo7s(orgNo7s);
        //org_no_7下所有台区数量
        List<TgInfoDO> allTgNumList = tgInfoMapper.selectTgNumByOrgNo7s(orgNo7s);
        //查询正常台区数量 按供电公司分组 status 1 为正常
        List<TgStatusDO> normalStatusDOS = tgStatusMapper.selectByTgStatusGroupByOrgNo7(statusParam);

        //查询异常台区数量 status 2 为异常
        statusParam.setStatus(2);
        List<TgStatusDO> errStatusDOS = tgStatusMapper.selectByTgStatusGroupByOrgNo7(statusParam);

        //新建台区 ndays < 300
        statusParam.setStatus(3);
        List<TgStatusDO> newStatusDOS = tgStatusMapper.selectByTgStatusGroupByOrgNo7(statusParam);

        Map<String, Integer> normalMap = new HashMap<>();
        Map<String, Integer> errMap = new HashMap<>();
        Map<String, Integer> newMap = new HashMap<>();

        if (!normalStatusDOS.isEmpty()) {
            normalMap = normalStatusDOS.stream().collect(Collectors.toMap(TgStatusDO::getORGNO, TgStatusDO::getTGNUM));
        }
        if (!errStatusDOS.isEmpty()) {
            errMap = errStatusDOS.stream().collect(Collectors.toMap(TgStatusDO::getORGNO, TgStatusDO::getTGNUM));
        }
        if (!newStatusDOS.isEmpty()) {
            newMap = newStatusDOS.stream().collect(Collectors.toMap(TgStatusDO::getORGNO, TgStatusDO::getTGNUM));
        }

        for (SaOrgDO saOrgDO : saOrgDOS) {
            PowerSupplyGroupByTgVO vo = new PowerSupplyGroupByTgVO();
            vo.setOrg_7_name(saOrgDO.getOrgName().trim());
            vo.setOrg_id(saOrgDO.getOrgNo());

            if (normalMap.containsKey(saOrgDO.getOrgNo())) {
                vo.setNormal_tg_num(normalMap.get(saOrgDO.getOrgNo()));
            }
            if (errMap.containsKey(saOrgDO.getOrgNo())) {
                vo.setAlarm_tg_num(errMap.get(saOrgDO.getOrgNo()));
            }
            if (newMap.containsKey(saOrgDO.getOrgNo())) {
                vo.setNew_tg_num(newMap.get(saOrgDO.getOrgNo()));
            }
            vo.setTotal_tg_num(vo.getNew_tg_num() + vo.getNormal_tg_num() + vo.getAlarm_tg_num());
            if (vo.getTotal_tg_num() > 0) {
                vo.setNormal_tg_rate(Math.round((float) vo.getNormal_tg_num() / vo.getTotal_tg_num() * 10000) / 100F);
                vo.setAlarm_tg_rate(Math.round((float) vo.getAlarm_tg_num() / vo.getTotal_tg_num() * 10000) / 100F);
                vo.setNew_tg_rate(Math.round((float) vo.getNew_tg_num() / vo.getTotal_tg_num() * 10000) / 100F);
            }
            data.add(vo);
        }
        // 总台区-异常 = 正常台区
        result.put("list", data);
        return result;
    }


    @Override
    public List<PowerSupplyGroupByTgVO> powerSupplyGroupByTgNoPage(String year, String scope, String orgNo7) {


        List<PowerSupplyGroupByTgVO> result = new ArrayList<>();


        SaOrgParam saOrgParam = new SaOrgParam();
        saOrgParam.setOrgNo7(orgNo7);

        //查询TgInfo
        if (StringUtils.isBlank(orgNo7)) {
            saOrgParam.setOrgType(4);
        }
        List<SaOrgDO> saOrgDOS = saOrgMapper.selectByParam(saOrgParam);
        if (saOrgDOS.isEmpty()) {
            return result;
        }


        TgStatusParam statusParam = new TgStatusParam();
        statusParam.setYear(year);
        statusParam.setOrgNo7s(saOrgDOS.stream().map(saOrg -> saOrg.getOrgNo().trim()).collect(Collectors.toList()));
        statusParam.setStatus(1);
        //查询正常台区数量 按供电公司分组 status 1 为正常
        List<TgStatusDO> normalStatusDOS = tgStatusMapper.selectByTgStatusGroupByOrgNo7(statusParam);

        //查询异常台区数量 status 2 为异常
        statusParam.setStatus(2);
        List<TgStatusDO> errStatusDOS = tgStatusMapper.selectByTgStatusGroupByOrgNo7(statusParam);

        //新建台区 ndays < 300
        statusParam.setStatus(3);
        List<TgStatusDO> newStatusDOS = tgStatusMapper.selectByTgStatusGroupByOrgNo7(statusParam);

        Map<String, Integer> normalMap = new HashMap<>();
        Map<String, Integer> errMap = new HashMap<>();
        Map<String, Integer> newMap = new HashMap<>();

        if (!normalStatusDOS.isEmpty()) {
            normalMap = normalStatusDOS.stream().collect(Collectors.toMap(tgStatus -> tgStatus.getORGNO().trim(), TgStatusDO::getTGNUM));
        }
        if (!errStatusDOS.isEmpty()) {
            errMap = errStatusDOS.stream().collect(Collectors.toMap(tgStatus -> tgStatus.getORGNO().trim(), TgStatusDO::getTGNUM));
        }
        if (!newStatusDOS.isEmpty()) {
            newMap = newStatusDOS.stream().collect(Collectors.toMap(tgStatus -> tgStatus.getORGNO().trim(), TgStatusDO::getTGNUM));
        }

        for (SaOrgDO saOrgDO : saOrgDOS) {
            PowerSupplyGroupByTgVO vo = new PowerSupplyGroupByTgVO();
            vo.setOrg_7_name(saOrgDO.getOrgName().trim());
            vo.setOrg_id(saOrgDO.getOrgNo().trim());
            if (normalMap.containsKey(saOrgDO.getOrgNo().trim())) {
                vo.setNormal_tg_num(normalMap.get(saOrgDO.getOrgNo().trim()));
            }
            if (errMap.containsKey(saOrgDO.getOrgNo().trim())) {
                vo.setAlarm_tg_num(errMap.get(saOrgDO.getOrgNo().trim()));
            }
            if (newMap.containsKey(saOrgDO.getOrgNo().trim())) {
                vo.setNew_tg_num(newMap.get(saOrgDO.getOrgNo().trim()));
            }
            result.add(vo);
        }
        return result;
    }


    @Override
    public List<MeterVO> powerSupplyGroupByTgMeterNumNoPage(String year, String scope, String orgNo7) {


        List<MeterVO> result = new ArrayList<>();


        TgStatusParam statusParam = new TgStatusParam();
        statusParam.setYear(year);
        statusParam.setOrgNo7(orgNo7);

        SaOrgParam saOrgParam = new SaOrgParam();
        saOrgParam.setOrgNo7(orgNo7);
        //查询Org
        if (StringUtils.isBlank(orgNo7)) {
            //查询orn7 的供电所
            saOrgParam.setOrgType(4);
        }
        List<SaOrgDO> saOrgDOS = saOrgMapper.selectByParam(saOrgParam);
        if (saOrgDOS.isEmpty()) {
            return result;
        }

        //tg_status left tg_info GROUP BY ORGnO 查询正常表异常表  观察表

        //查询正常台区数量 按供电公司分组 status 1 为正常
        List<TgStatusDO> normalStatusDOS = tgStatusMapper.selectByParamGroupByOrgNo7(statusParam);

        Map<String, TgStatusDO> meterNumMap = new HashMap<>();

        if (!normalStatusDOS.isEmpty()) {
            meterNumMap = normalStatusDOS.stream().collect(Collectors.toMap(TgStatusDO::getORGNO, Function.identity()));
        }

        for (SaOrgDO saOrgDO : saOrgDOS) {
            MeterVO vo = new MeterVO();
            vo.setOrg_7_name(saOrgDO.getOrgName().trim());
            vo.setOrg_7_id(saOrgDO.getOrgNo().trim());
            if (meterNumMap.containsKey(saOrgDO.getOrgNo())) {
                TgStatusDO tgStatusDO = meterNumMap.get(saOrgDO.getOrgNo());
                //可算-超差
                vo.setNormal_meter_num(tgStatusDO.getNIMETERS() - tgStatusDO.getNEMETERS());
                //超差
                vo.setInvalid_meter_num(tgStatusDO.getNEMETERS());
                //总 - 可算 = 观察
                vo.setTest_meter_num(tgStatusDO.getNMETERS() - tgStatusDO.getNIMETERS());
            }
            result.add(vo);
        }
        return result;
    }

    @Override
    public Map<String, Object> getErrorByOrgNameLike(String year, String scope, String orgNo7, Integer page, Integer size, String keyword) {

        Map<String, Object> result = new HashMap<>();
        TgInfoParam param = new TgInfoParam();
        param.setYear(year);
        //param.setScope(scope);
        param.setOrgNo7(orgNo7);
        param.setOrgNameLike(keyword);
        param.setSize(size);
        param.setPage((page - 1) * size);
        param.setTgStatus(2);
        List<TgInfoDO> dos = tgInfoMapper.selectTgInfoByParam(param);
        int count = tgInfoMapper.selectTgInfoByParamCount(param);

        List<ErrorByOrgNameLikeVO> data = new ArrayList<>();
        if (!dos.isEmpty()) {
            dos.stream().forEach(tgInfoDO -> {
                //errorInfo.setAlarm_name(ExceptionCodeEnum.getName(errorDO.getAlarmCode()));
                data.add(BeanUtil.tgInfo2ErrorNameLikeVO(tgInfoDO));
            });
        }
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));

        result.put("list", data);
        return result;
    }

    @Override
    public Map<String, Object> getBaseInfoByTgId(String tgid, String year) {
        Map<String, Object> result = new HashMap<>();

        //查询台区信息  需set 台区电表总数 从tg_status取值
        TgArchiveInfoVO tgArchiveInfoVO = this.getTgArchiveInfo(tgid);

        //电能表计量误差信

        //查询error数量

        //int errorNum = tErrorComputingMapper.getErrorNumByTgId(tgid);

        List<MpInfoVO> mpInfoVOList = new ArrayList<>();
        result.put("mp_info", mpInfoVOList);
        TgPowerVO tgPowerVO = new TgPowerVO();
        /*// 台区用电信息
        PageHelper.startPage(1,1);
        //查询第一天冻结日期
        List<TgDailyFreezeDO> freezeDOS = tgDailyFreezeMapper.selectByTgIdOrderByTime(tgid, 1,null);

        if (freezeDOS.isEmpty()) {
            result.put("tg_power_info", freezeVO);
            return result;
        }
        //获取第一天日期
        String firstDay = freezeDOS.get(0).getDATADATE();
        freezeVO.setFreeze_start_day(freezeDOS.get(0).getDATADATE());
        //查询最后一天冻结日期
        PageHelper.startPage(1,1);
        //查询第一天冻结日期 如果第一天有值 最后一天一定有值
        List<TgDailyFreezeDO> latestFreezeList = tgDailyFreezeMapper.selectByTgIdOrderByTime(tgid, null, 1);

        //获取最后一天日期
        String datadate = latestFreezeList.get(0).getDATADATE();

        //获取 最后一天 tgid 下冻结的所有数据
        List<TgDailyFreezeDO> freezeDOList = tgDailyFreezeMapper.selectListByTgIdAndTime(tgid, datadate);

        //计算 时间差值
        Calendar firstCalendar = Calendar.getInstance();
        firstCalendar.setTime(new Date(firstDay));
        Calendar latestCalendar = Calendar.getInstance();
        latestCalendar.setTime(new Date(datadate));

        int dayNum = latestCalendar.DAY_OF_YEAR - firstCalendar.DAY_OF_YEAR;
        //获取年份差
        dayNum = (latestCalendar.YEAR - firstCalendar.YEAR) * 365 + dayNum;

        //计算全部用电量
        Float sumElectric = freezeDOList.stream().map(TgDailyFreezeDO::getPAP_R).reduce(Float::sum).get();

        float singleDayElectric = Math.round(sumElectric / dayNum * 10000) / 10000f;

        freezeVO.setTotal_power(singleDayElectric + "kwh");
        freezeVO.setPower(singleDayElectric + "kwh");
        freezeVO.setChange_rate(latestFreezeList.get(0).getRATEDCURRENT());*/

        //查询tg_status 数据
        TgStatusDO tgStatusDO = tgStatusService.selectByTgId(tgid, year);
        tgPowerVO.setPower(String.valueOf(tgStatusDO.getPOWERAVG()));
        //TODO 日均总用电量
        //tgPowerVO.setTotal_power();
        tgPowerVO.setChange_rate(String.valueOf(tgStatusDO.getLOSSAVG()));

        MeterArchiveInfoVO meterArchiveInfoVO = new MeterArchiveInfoVO();
        //查询 meter_status
        List<MeterStatusDO> meterStatusDOS = meterStatusMapper.selectByTgIdGroupByStatus(tgid, null);
        if (!meterStatusDOS.isEmpty()) {
            for (MeterStatusDO meterStatusDO : meterStatusDOS) {
                if (meterStatusDO.getSTATUS() == 1) {
                    //正常
                    meterArchiveInfoVO.setValid_meter_num(meterStatusDO.getMeterNum());
                    break;
                }
            }
        }

        meterArchiveInfoVO.setMeter_num(tgStatusDO.getNMETERS());//台区总表数量
        meterArchiveInfoVO.setInvalid_meter_num(tgStatusDO.getNEMETERS());
        meterArchiveInfoVO.setTest_meter_num(0);

        //添加 台区下电能表总数量
        tgArchiveInfoVO.setMeter_num(tgStatusDO.getNMETERS());

        //台区电能表信息
        TgMeterInfoVO tgMeterInfoVO = new TgMeterInfoVO();
        //电能表总数量
        tgArchiveInfoVO.setMeter_num(tgStatusDO.getNMETERS());
        //正常表数  查询meter_status中 status为 1 正常的count
        List<MeterStatusDO> meterStatusDOList = meterStatusMapper.selectByTgIdGroupByStatus(tgid, null);
        int validNum = meterStatusDOList.stream().filter(meterStatusDO -> meterStatusDO.getSTATUS() == 1).collect(Collectors.toSet()).size();

        tgMeterInfoVO.setValid_meter_num(validNum);

        //可算表 包括 正常+异常
        tgMeterInfoVO.setInvalid_meter_num(tgStatusDO.getNEMETERS());
        result.put("tg_meter_info", meterArchiveInfoVO);
        result.put("tg_archive_info", tgArchiveInfoVO);
        result.put("tg_power_info", tgPowerVO);
        result.put("tg_meter_info", tgMeterInfoVO);
        return result;
    }

    @Override
    public Map<String, Object> errorNumChangeByTgIdOrMeterId(String orgNo7, String year) throws Exception {
        Map<String, Object> result = new HashMap<>();

        //查看区间范围内 异常类型
        //选中日期往前推7天

        List<List<Integer>> yaxis = new ArrayList<>();
        List<String> xaxis = new ArrayList<>();

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(format.parse(year));
        startCalendar.add(Calendar.DAY_OF_YEAR, -7);

        Calendar endtimeCalendar = Calendar.getInstance();
        endtimeCalendar.setTime(format.parse(year));

        String startime = format.format(startCalendar.getTime());
        String endtime = format.format(endtimeCalendar.getTime());

        List<String> titleCodeList = tErrorComputingMapper.selectErrorCodeByTgIdOrMeterId(startime, endtime, null, orgNo7);
        List<String> alarmName = new ArrayList<>();
        if (!titleCodeList.isEmpty()) {

            Calendar tempCalendar = Calendar.getInstance();
            tempCalendar.setTime(startCalendar.getTime());
            tempCalendar.add(Calendar.DAY_OF_MONTH, 1);
            //初始化 ysxis 中对象
            titleCodeList.stream().forEach(title -> {
                yaxis.add(new ArrayList<>());
            });

            while (tempCalendar.getTime().compareTo(endtimeCalendar.getTime()) >= 0) {
                //如果开始时间 小于 结束时间  则查询
                String start = format.format(startCalendar.getTime());
                String end = format.format(tempCalendar.getTime());

                List<TErrorComputingResultsDO> errList = tErrorComputingMapper.selectByAlarmCodeByTgId(start, end, null, orgNo7);
                Map<String, Integer> errMap = new HashMap<>();

                if (!errList.isEmpty()) {
                    errMap = errList.stream().collect(Collectors.toMap(errorDO -> errorDO.getAlarmCode(), errorDO -> errorDO.getAlarmNum()));
                }
                for (int i = 0; i < titleCodeList.size(); i++) {
                    String errCode = titleCodeList.get(i);
                    if (errMap.containsKey(errCode)) {
                        yaxis.get(i).add(errMap.get(errCode));
                    } else {
                        yaxis.get(i).add(0);
                    }

                }
                xaxis.add(dateFormat.format(tempCalendar.getTime()));
                tempCalendar.add(Calendar.DAY_OF_MONTH, 1);
                startCalendar.add(Calendar.DAY_OF_MONTH, 1);
            }

            titleCodeList.stream().forEach(alramCode -> {
                alarmName.add(ExceptionCodeEnum.getName(alramCode));
            });
        }
        result.put("title", alarmName);
        result.put("xaxis", xaxis);
        result.put("yaxis", yaxis);
        return result;
    }

    @Override
    public Map<String, Object> alarmOrg(String year, String scope, String orgNo7, Boolean isTgid) {
        Map<String, Object> result = new HashMap<>();

        // 最多展示5组
        List<TErrorComputingResultsDO> doList = new ArrayList<>();
        ErrorComputingParam param = new ErrorComputingParam();
        param.setOrgNo7(orgNo7);
        param.setYear(year);
        param.setPage(0);
        param.setSize(5);
        if (isTgid) {
            doList = tErrorComputingMapper.selectErrorNumGroupByON7DistinctTgid(param);
        } else {
            doList = tErrorComputingMapper.selectErrorNumGroupByON7(param);
        }

        String day = dateFormat.format(new Date());
        List<String> title = new ArrayList<>();
        result.put("day", day);
        List<Integer> numList = new ArrayList<>();
        if (doList.isEmpty()) {
            result.put("total_err_num", 0);
            result.put("list", numList);
            result.put("title", title);
            return result;
        }
        //查询orgName
        List<SaOrgDO> saOrgDOS = saOrgMapper.selectByOrgNoList(doList.stream().map(err -> err.getON7()).collect(Collectors.toList()));
        Map<String, String> orgMap = new HashMap<>();
        if (!saOrgDOS.isEmpty()) {
            orgMap = saOrgDOS.stream().collect(Collectors.toMap(org -> org.getOrgNo().trim(), SaOrgDO::getOrgName));
        }
        for (TErrorComputingResultsDO errDO : doList) {
            numList.add(errDO.getAlarmNum());
            if (orgMap.containsKey(errDO.getON7())) {
                title.add(orgMap.get(errDO.getON7()).trim());
            }
        }
        result.put("total_err_num", doList.stream().map(err -> err.getAlarmNum()).reduce(Integer::sum));
        result.put("list", numList);
        result.put("title", title);
        return result;
    }

    @Override
    public List<MeterPowerVO> electricMeterPowerByMeterid(String meterid, Integer isallmeter, String tgid, String year) throws ParseException {

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(format.parse(year));
        startCalendar.add(Calendar.DAY_OF_YEAR, -7);//当前时间-7
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(format.parse(year));
        String startTime = format.format(startCalendar.getTime());
        String endTime = year;

        //根据meterid查询用电量  理论上一个meterID只能对应一个 tg_id

        List<MeterStatusDO> meterStatusDOS = meterStatusMapper.selectMeterPowerByMeterId(meterid, startTime, endTime);

        List<MeterPowerVO> list = new ArrayList<>();

        if (meterStatusDOS.isEmpty()) {
            return list;
        }
        Map<String, MeterStatusDO> sumMap = new HashMap<>();
        if (isallmeter == 1) {
            //按时间倒序
            //如果展示台区总电量
            List<MeterStatusDO> sumFreezeList = meterStatusMapper.selectTgPowerByTgId(meterStatusDOS.get(0).getTGID(), startTime, endTime);
            if (!sumFreezeList.isEmpty()) {
                sumMap = sumFreezeList.stream().collect(Collectors.toMap(sumFreeze -> format.format(sumFreeze.getDATE()), Function.identity()));
            }
        }

        while (startCalendar.compareTo(endCalendar) <= 0) {
            MeterPowerVO powerVO = new MeterPowerVO();
            String time = format.format(startCalendar.getTime());
            float curPower = 0f;
            for (MeterStatusDO meterStatusDO : meterStatusDOS) {
                if (format.format(meterStatusDO.getDATE()).equals(time)) {
                    curPower += meterStatusDO.getPOWERAVG();
                    break;
                }
            }
            powerVO.setTime(time);
            powerVO.setCur_power(String.valueOf(curPower));
            if (sumMap.containsKey(time)) {
                powerVO.setAll_power(String.valueOf(sumMap.get(time).getPOWERAVG()));
            }
            list.add(powerVO);
            startCalendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        return list;
    }

    @Override
    public List<MeterPowerVO> electricMeterPowerByTgid(String tgid) {

        List<MeterPowerVO> list = new ArrayList<>();
        Map<Date, MeterStatusDO> maxPowerMap = new HashMap<>();
        //展示台区总电量
        List<MeterStatusDO> sumFreezeList = meterStatusMapper.selectTgPowerByTgId(tgid, null, null);
        if (sumFreezeList.isEmpty()) {
            return list;
        }
        //查询最大用户量
        String startTime = format.format(sumFreezeList.get(0).getDATE());
        String endTime = format.format(sumFreezeList.get(sumFreezeList.size() - 1).getDATE());
        List<MeterStatusDO> maxPowerList = meterStatusMapper.selectMaxPowerByTgid(tgid, startTime, endTime);
        if (!maxPowerList.isEmpty()) {
            maxPowerMap = maxPowerList.stream().collect(Collectors.toMap(maxpower -> maxpower.getDATE(), Function.identity()));
        }
        for (MeterStatusDO sumFreeze : sumFreezeList) {
            MeterPowerVO powerVO = new MeterPowerVO();
            powerVO.setAll_tg_power(String.valueOf(sumFreeze.getPOWERAVG()));
            powerVO.setAll_cons_power(String.valueOf(sumFreeze.getPOWERAVG()));
            powerVO.setTime(format.format(sumFreeze.getDATE()));
            if (maxPowerMap.containsKey(sumFreeze.getDATE())) {
                powerVO.setMax_cons_power(String.valueOf(maxPowerMap.get(sumFreeze.getDATE()).getPOWERAVG()));
            }
            list.add(powerVO);
        }
        return list;
    }

    @Override
    public Map<String, Object> meterRunningErrorByTgId(String tgid) {

        //查询台区下所有err电能表
        List<TErrorComputingResultsDO> errList = tErrorComputingMapper.selectByTgId(tgid);

        Map<String, Object> result = new HashMap<>();
        //展示台区总电量
        if (errList.isEmpty()) {
            return result;
        }
        Map<String, List<TErrorComputingResultsDO>> meterMap = new HashMap<>();
        List<String> titles = errList.stream().map(error -> error.getMeterId()).distinct().collect(Collectors.toList());
        result.put("titles", titles);
        List<String> xList = errList.stream().map(error -> format.format(error.getGenDate())).distinct().collect(Collectors.toList());
        result.put("xList", xList);
        //按日期分组
        meterMap = errList.stream().collect(Collectors.groupingBy(error -> error.getMeterId(), Collectors.toList()));

        List<List<Double>> yList = new ArrayList<>();
        for (String meterid : titles) {
            List<Double> errPercent = new ArrayList<>();
            List<TErrorComputingResultsDO> singleMeterList = meterMap.get(meterid);
            Map<String, TErrorComputingResultsDO> singleMerMap = new HashMap<>();
            if (!singleMeterList.isEmpty()) {
                singleMerMap = singleMeterList.stream().collect(Collectors.toMap(error -> format.format(error.getGenDate()), Function.identity()));
            }
            for (String date : xList) {
                if (singleMerMap.containsKey(date)) {
                    errPercent.add(singleMerMap.get(date).getErrorPercent());
                } else {
                    errPercent.add(0d);
                }
            }
            yList.add(errPercent);
        }
        result.put("yList", yList);
        return result;
    }

    @Override
    public Map<String, Object> meterRunningError(String meterid, String year) throws ParseException {
        Map<String, Object> result = new HashMap<>();

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(format.parse(year));
        startCalendar.add(Calendar.DAY_OF_YEAR, -7);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(format.parse(year));

        String startime = format.format(startCalendar.getTime());
        String endtime = format.format(endCalendar.getTime());

        List<TErrorComputingResultsDO> dos = tErrorComputingMapper.selectByMeterIdOrderByTime(meterid, startime, endtime);
        List<Map<String, String>> data = new ArrayList<>();

        if (dos.isEmpty()) {
            result.put("list", data);
            return result;
        }

        while (startCalendar.compareTo(endCalendar) <= 0) {
            String time = format.format(startCalendar.getTime());
            double errE = 0d;
            Map<String, String> map = new HashMap<>();
            for (TErrorComputingResultsDO errDO : dos) {
                if (time.equals(format.format(errDO.getGenDate()))) {
                    errE = errDO.getErrorE();break;
                }
            }
            map.put("time", time);
            map.put("value", String.valueOf(errE));
            data.add(map);
            startCalendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        result.put("list", data);
        return result;
    }

    @Override
    public Map<String, Object> selectNewTgByOrgName(String orgName, int page, int size, String orgNo7) {

        //查询新建台区信息
        //tgInfoMapper.selectNewTgByOrgNameLike()

        TgInfoParam tgInfoParam = new TgInfoParam();
        tgInfoParam.setSize(size);
        tgInfoParam.setOrgNo7(orgNo7);
        tgInfoParam.setPage((page - 1) * size);
        tgInfoParam.setOrgNameLike(orgName);
        tgInfoParam.setTgStatus(3);
        tgInfoParam.setNDays(300);
        Map<String, Object> result = new HashMap<>();
        List<TgInfoDO> dos = tgInfoMapper.selectTgInfoByParam(tgInfoParam);
        int count = tgInfoMapper.selectTgInfoByParamCount(tgInfoParam);
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));
        List<NewTgByNameLikeVO> data = new ArrayList<>();

        if (dos.isEmpty()) {
            result.put("list", data);
            return result;
        }

        dos.stream().forEach(tgInfoDO -> {
            data.add(BeanUtil.tgInfo2NewTgByNameLikeVO(tgInfoDO));
        });

        result.put("list", data);
        return result;
    }

    @Override
    public Map<String, Object> selectAllTgByOrgName(String keyword, String tgStatus, int page, int size, String orgNo7) {
        //查询新建台区信息
        //tgInfoMapper.selectNewTgByOrgNameLike()

        TgInfoParam tgInfoParam = new TgInfoParam();
        tgInfoParam.setSize(size);
        tgInfoParam.setOrgNo7(orgNo7);
        tgInfoParam.setPage((page - 1) * size);
        tgInfoParam.setOrgNameLike(keyword);
        if (null == tgStatus) {
        } else if (Integer.valueOf(tgStatus) == 1) {
            //正常台区
            tgInfoParam.setTgStatus(1);
        } else if (Integer.valueOf(tgStatus) == 2) {
            //异常台区
            tgInfoParam.setTgStatus(2);
        } else if (Integer.valueOf(tgStatus) == 3) {
            //新建台区
            tgInfoParam.setNDays(300);
        }
        tgInfoParam.setNDays(300);
        Map<String, Object> result = new HashMap<>();
        List<TgInfoDO> dos = tgInfoMapper.selectTgInfoByParam(tgInfoParam);
        int count = tgInfoMapper.selectTgInfoByParamCount(tgInfoParam);
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));
        List<AllTgByNameLikeVO> data = new ArrayList<>();

        if (dos.isEmpty()) {
            result.put("list", data);
            return result;
        }

        dos.stream().forEach(tgInfoDO -> {
            data.add(BeanUtil.tgInfo2AllTgByNameLikeVO(tgInfoDO));
        });

        result.put("list", data);
        return result;
    }

    @Override
    public Map<String, Object> selectNormalTgByOrgName(String keyword, int page, int size, String orgNo7) {
        Map<String, Object> result = new HashMap<>();
        TgInfoParam param = new TgInfoParam();
        param.setOrgNo7(orgNo7);
        param.setOrgNameLike(keyword);
        param.setSize(size);
        param.setPage((page - 1) * size);
        param.setTgStatus(1);
        List<TgInfoDO> dos = tgInfoMapper.selectTgInfoByParam(param);
        int count = tgInfoMapper.selectTgInfoByParamCount(param);

        List<NormalTgByNameLikeVO> data = new ArrayList<>();
        if (!dos.isEmpty()) {
            dos.stream().forEach(tgInfoDO -> {
                data.add(BeanUtil.tgInfo2NormalTgByNameLikeVO(tgInfoDO));
            });
        }
        //当前页
        result.put("cur_page", page);
        //每页数量
        result.put("page_size", size);
        //总页数
        result.put("total_page", (int) Math.ceil((float) count / size));

        result.put("list", data);
        return result;
    }

    @Override
    public Map<String, Object> getAlramMeterNumByAlramCodeAndON7(String alarmCode, String orgNo7) {

        Map<String,Object> result = new HashMap<>();
        List<TErrorComputingResultsDO> dos = tErrorComputingMapper.selectByAlramCodeAndON7OrderByGenDate(alarmCode, orgNo7);

        List<String> xList = new ArrayList<>();
        List<Integer> dataList = new ArrayList<>();
        if (!dos.isEmpty()) {
            xList.addAll(dos.stream().map(dto -> dto.getDateDayzi()).collect(Collectors.toList()));
            dataList.addAll(dos.stream().map(dto -> dto.getAlarmNum()).collect(Collectors.toList()));
        }
        result.put("xList",xList);
        result.put("dataList",dataList);

        return result;
    }


    /**
     * 回参 没有台区电能表总数
     *
     * @param tgid
     * @return
     */
    public TgArchiveInfoVO getTgArchiveInfo(String tgid) {
        //查询台区信息
        TgInfoDO tgInfoDO = tgInfoMapper.selectByTgId(tgid);

        if (tgInfoDO == null) {
            return new TgArchiveInfoVO();
        }
        TgArchiveInfoVO result = BeanUtil.tgArchiveDO2VO(tgInfoDO);

        //获取 台区城市代码
        List<String> cityList = new ArrayList<>();


        cityList.add(tgInfoDO.getON5());
        cityList.add(tgInfoDO.getON7());

        List<SaOrgDO> saOrgDOS = saOrgMapper.selectByOrgNoList(cityList);
        if (!saOrgDOS.isEmpty()) {
            saOrgDOS.stream().forEach(saOrgDO -> {
                if (saOrgDO.getOrgNo().equals(tgInfoDO.getON5())) {
                    result.setTg_city(saOrgDO.getOrgName().trim());
                } else {
                    result.setTg_county(saOrgDO.getOrgName().trim());
                }
            });
        }
        return result;
    }

}
